<?php 
class Website extends CS_Controller
{   
    private $pageBelongArray;
    public function _init()
    {
        $this->load->helper(array('common'));
        $this->load->library('pagination');
        $this->load->model('website_model','website');
        $this->pageBelongArray = pageBelongArray();
    }
    
    public function grid($pg=1)
    {
        $pageNum = 20;
        $getData = $this->input->get();
        $num = ($pg-1)*$pageNum;
        $config['first_url']   = base_url('website/grid').$this->pageGetParam($this->input->get());
        $config['suffix']      = $this->pageGetParam($this->input->get());
        $config['base_url']    = base_url('website/grid');
        $config['total_rows']  = $this->website->total($getData);
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link']   = $this->pagination->create_links();
        $data['page_list'] = $this->website->searchList($pageNum,$num,$getData);
        $data['all_rows']  = $config['total_rows'];
        $data['pg_now']    = $pg;
        $data['pageBelongArray'] = $this->pageBelongArray;
        $this->load->view('website/grid',$data);
    }

    public function add()
    {
        $data['pageBelongArray'] = $this->pageBelongArray;
        $this->load->view('website/add',$data);
    }
    
    public function addPost()
    {
        $error = $this->validate();
        if (!empty($error)) {
            $this->error('website/add', '', $error);
        }
        $this->db->trans_start();
        $resultId = $this->website->insertWebsite($this->input->post());
        $this->db->trans_complete();
        if ($resultId) {
            $this->success('website/grid', '', '保存成功！');
        } else {
            $this->error('website/add', '', '保存失败！');
        }
    }
    
    public function edit($id)
    {
        $result = $this->website->findById($id);
        if($result->num_rows() <= 0) {
            $this->redirect('website/grid');
        }
        $data['website'] = $result->row();
        $data['pageBelongArray'] = $this->pageBelongArray;
        $this->load->view('website/edit', $data);
    }
    
    public function editPost()
    {
        $listId = $this->input->post('list_id');
        $error = $this->validate();
        if (!empty($error)) {
            $this->error('website/edit',$listId,$error);
        }
        
        $this->db->trans_start();
        $resultId = $this->website->updateWebsite($this->input->post());
        $this->db->trans_complete();
    
        if ($resultId) {
            $this->success('website/grid', '', '保存成功！');
        } else {
            $this->error('website/edit', $listId, '保存失败！');
        }
    }

    public function delete($id)
    {
        $is_delete = $this->website->deleteByListId($id);
        if ($is_delete) {
            $this->success('website/grid', '', '删除成功！');
        } else {
            $this->error('website/grid', '', '删除失败！');
        }
    }
    
    public function validate()
    {
        $error = array();
        if ($this->validateParam($this->input->post('list_name'))) {
            $error[] = '标题不能为空';
        }
        if ($this->validateParam($this->input->post('list_content'))) {
            $error[] = '内容不能为空';
        }
        return $error;
    }
}